package com.fita.entity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import com.fita.entity.data.ExchangeRate;
import com.fita.entity.dto.ehi.response.Price;
import com.fita.entity.dto.ehi.response.VehicleCore;
import com.fita.entity.order.PayType;
import com.fita.entity.set.SetPayType;

/**
 * @description:
 * @author: 11200
 * @date: 2019/7/12
 */
public class CarPriceHelper {

    public static List<Price> generatePrice(VehicleCore vehicleCore, int agentRate, Integer primaryAgentRate, PayType payType, ExchangeRate exchangeRate) {

        List<Price> prices = new ArrayList<>();

        BigDecimal totalAmount = vehicleCore.getTotalAmount();

        if (payType.equals(PayType.ARRIVEPAY)) { // 到付

            // 到付的官方零售价
            Price retailPrice = new Price(exchangeRate.getCurrencySymbol(), "官方零售价", totalAmount);

            prices.add(retailPrice);
            // 代理返佣
            BigDecimal agentCommission = totalAmount.multiply(new BigDecimal(agentRate).divide(new BigDecimal(100)));

            Price agentCommissionPrice = new Price(exchangeRate.getCurrencySymbol(), "代理返佣", agentCommission);

            // 一级代理返点
            if (primaryAgentRate != null && primaryAgentRate > 0) {
                BigDecimal subtract = new BigDecimal(1).subtract(new BigDecimal(primaryAgentRate).divide(new BigDecimal(100)));
                agentCommission = agentCommission.multiply(subtract);
                // 更新代理返佣
                agentCommissionPrice.setPrice(agentCommission);
            }

            prices.add(agentCommissionPrice);

            return prices;

        } else if (payType.equals(PayType.PREPAY)) {  // 预付

            // 官方零售价
            Price retailPrice = null;

            // 代理常规价
            Price agentCommonPrice = null;

            // 代理浮动价
            Price agentDynamicPrice = null;

            // 设置官方零售价
            BigDecimal destinationPaid = null;
            SetPayType set1PayType = vehicleCore.getSet1PayType();
            if (set1PayType.isDynamicPrepay()) {
                destinationPaid = totalAmount.multiply(new BigDecimal(1.176)).multiply(exchangeRate.getRate());
            } else {
                destinationPaid = totalAmount.multiply(exchangeRate.getRate());
            }
            retailPrice = new Price(exchangeRate.getCurrencySymbol(), "官方零售价", destinationPaid);

            // 设置代理浮动价
            //(1-返点率)
            BigDecimal currentRate = new BigDecimal(1).subtract(new BigDecimal(agentRate).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP));
            ;
            BigDecimal prepaid = null;
            if (set1PayType.isFloatPrepay()) {  // API*(1-返点率)
                prepaid = totalAmount.multiply(currentRate);
                prepaid = prepaid.multiply(exchangeRate.getRate()).setScale(0, RoundingMode.UP);
                agentDynamicPrice = new Price(exchangeRate.getCurrencySymbol(), "代理浮动价", prepaid);
            } else if (set1PayType.isDynamicPrepay()) { //API*117.6%*(1-返点率)
                prepaid = totalAmount.multiply(new BigDecimal(1.176)).multiply(currentRate);
                prepaid = prepaid.multiply(exchangeRate.getRate()).setScale(0, RoundingMode.UP);
                agentDynamicPrice = new Price(exchangeRate.getCurrencySymbol(), "代理浮动价", prepaid);
            }

            // 设置代理常规价
            BigDecimal dynamicPrepaid = totalAmount.multiply(new BigDecimal(1.25)).multiply(currentRate).multiply(exchangeRate.getRate()).setScale(0, RoundingMode.UP);

            agentCommonPrice = new Price(exchangeRate.getCurrencySymbol(), "代理浮动价", dynamicPrepaid);

            // 如果当前所选套餐两种价格都有, 重新计算官方零售价和代理浮动价
            if (!vehicleCore.isEmptyPrice2()) {

                BigDecimal totalAmount2 = vehicleCore.getTotalAmount2();

                // 重新设置官方零售价
                BigDecimal destinationPaid2 = null;
                SetPayType set2PayType = vehicleCore.getSet2PayType();
                if (set2PayType.isDynamicPrepay()) {
                    destinationPaid2 = totalAmount2.multiply(new BigDecimal(1.176)).multiply(exchangeRate.getRate());
                } else {
                    destinationPaid2 = totalAmount2.multiply(exchangeRate.getRate());
                }
                retailPrice.setPrice(destinationPaid2);

                // 重新设置代理浮动价
                //(1-返点率)
                BigDecimal prepaid2 = null;
                if (set2PayType.isFloatPrepay()) {  // API*(1-返点率)
                    prepaid2 = totalAmount2.multiply(currentRate);
                } else if (set2PayType.isDynamicPrepay()) { //API*117.6%*(1-返点率)
                    prepaid2 = totalAmount2.multiply(new BigDecimal(1.176)).multiply(currentRate);
                }
                prepaid2 = prepaid2.multiply(exchangeRate.getRate());
                agentDynamicPrice.setPrice(prepaid2);
            }

            if (retailPrice != null) {
                prices.add(retailPrice);
            }
            if (agentCommonPrice != null) {
                prices.add(agentCommonPrice);
            }
            if (agentDynamicPrice != null) {
                prices.add(agentDynamicPrice);
            }
        }
        return prices;
    }

}
